package com.hos.operation.api;


import com.hos.db.entity.UserDocEntity;
import com.hos.db.entity.UserPatsEntity;
import com.hos.db.entity.UserSysEntity;
import com.hos.db.entity.ill.IllBuildEntity;
import com.hos.db.entity.ill.IllCureEvtity;
import com.hos.db.entity.ill.IllDataEntity;
import com.hos.db.entity.ill.IllHistoryEntity;
import com.hos.db.entity.ill.IllOptionEntity;
import com.hos.db.entity.ill.IllResultEntity;
import com.hos.db.entity.ill.IllsEntity;
import com.hos.db.manager.IllsManager1;
import com.hos.net.DictionariesFileService;
import com.hos.net.DictionariesIllService;
import com.hos.net.req.file.UpLoadingReq;
import com.hos.net.req.ill1.IllAddReq;
import com.hos.net.req.ill1.IllOptionReq;
import com.hos.net.req.ill1.IllResultReq;
import com.hos.net.req.ill1.IllUpdateReq;
import com.hos.net.res.BaseResponse;
import com.hos.net.res.MsgHint;
import com.hos.net.res.PageData;
import com.hos.net.res.loading.UploadALiRes;
import com.hos.operation.manager.ConstantData;
import com.hos.utile.JsonUtile;
import com.hos.utile.Md5Utile;
import com.hos.utile.StringUtile;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

//共用的api（病历，上传）
@Controller
public class ApiBaseCommunal extends ApiBase {

    //病历性相关接口
    protected String onIll(String service, String json, UserDocEntity doc, UserPatsEntity pat, UserSysEntity sys) {
        String response = "";
        switch (service) {
            case DictionariesIllService.HOS_ILL_ADD:
                //添加病历
                IllAddReq illReq = (IllAddReq) JsonUtile.json2Obj(json,
                        IllAddReq.class);
                IllsEntity ill = illReq.ill;
                if (ill == null) {
                    response = new MsgHint("-1", "患者id不能为空").toJson();
                    break;
                }
                if (StringUtile.isEmpty(ill.getPatId())) {
                    response = new MsgHint("-1", "所有者id不能为空").toJson();
                    break;
                }
                if (StringUtile.isEmpty(ill.getCreateId())) {
                    response = new MsgHint("-1", "所有者id不能为空").toJson();
                    break;
                }
                String docId = "";
                String docName = "";
                if (doc != null) {
                    docId = doc.getDocId();
                    docName = doc.getDocName();
                }
                response = IllsManager1.getInstance().onIllAddReq(getJdbcTemplate(), docId,
                        docName, illReq);
                break;
            case DictionariesIllService.HOS_ILL_UPDATE:
                //更新
                IllUpdateReq illUpdateReq = (IllUpdateReq) JsonUtile.json2Obj(json,
                        IllUpdateReq.class);
                if (StringUtile.isEmpty(illUpdateReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                response = IllsManager1.getInstance().onIllUpdateReq(getJdbcTemplate(), illUpdateReq);
                break;
            case DictionariesIllService.HOS_ILL_DELETE:
                //删除
                illUpdateReq = (IllUpdateReq) JsonUtile.json2Obj(json,
                        IllUpdateReq.class);
                if (StringUtile.isEmpty(illUpdateReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                response = IllsManager1.getInstance().onIllDeleteReq(getJdbcTemplate(), illUpdateReq.illId);
                break;
            case DictionariesIllService.HOS_ILL_DETAILS:
                //病历获取
                IllResultReq illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.patId)) {
                    response = new MsgHint("-1", "患者id不能为空").toJson();
                    break;
                }

                List<IllsEntity> ills = IllsManager1.getInstance().getIlls(getJdbcTemplate(),
                        illResultReq.patId, illResultReq.pageNo, illResultReq.pageSize, true);
                int patSize = ills.size();
                BaseResponse baseRes = new BaseResponse();
                PageData pageData = new PageData();
                if (patSize > illResultReq.pageSize) {
                    //有下一页
                    ills.remove(patSize - 1);
                    pageData.hasNextPage = true;
                }
                pageData.pageNo = illResultReq.pageNo;
                pageData.pageSize = ills.size();
                baseRes.pageData = pageData;
                baseRes.list = ills;
                pageData.total = IllsManager1.getInstance().getIllSize(getJdbcTemplate(),
                        illResultReq.patId);

                response = JsonUtile.obj2Json(baseRes);
                break;

            case DictionariesIllService.HOS_ILL_DETAILS_BUILD:
                //体格信息
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                IllBuildEntity buildRes = IllsManager1.getInstance().getIllBuild(getJdbcTemplate(), illResultReq.illId);
                //
                BaseResponse res = new BaseResponse();
                res.obj = buildRes;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_DETAILS_DATA:
                //影像资料
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                IllDataEntity dataRes = IllsManager1.getInstance().getIllData(getJdbcTemplate(), illResultReq.illId);
                //
                res = new BaseResponse();
                res.obj = dataRes;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_DETAILS_CURE:
                //病历获取 治疗信息
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                IllCureEvtity cureRes = IllsManager1.getInstance().getCureEvtity(getJdbcTemplate(), illResultReq.illId);
                //
                res = new BaseResponse();
                res.obj = cureRes;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_DETAILS_RESULT:
                //病历获取 病情治疗结果/出院情况
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                IllResultEntity resultRes = IllsManager1.getInstance().getIllResult(getJdbcTemplate(), illResultReq.illId);
                //
                res = new BaseResponse();
                res.obj = resultRes;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_DETAILS_HISTORYS:
                //病历获取 病史
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                IllHistoryEntity historyRes = IllsManager1.getInstance().getIllHistory(getJdbcTemplate(), illResultReq.illId);
                //
                res = new BaseResponse();
                res.obj = historyRes;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_OPTION:
                //病历获取 选择
                IllOptionReq illOptionReq = (IllOptionReq) JsonUtile.json2Obj(json,
                        IllOptionReq.class);
                List<IllOptionEntity> options = IllsManager1.getInstance().getIllOptions(
                        getJdbcTemplate(), illOptionReq.ids);
                res = new BaseResponse();
                res.list = options;
                response = res.toJson("0", "");
                break;
            case DictionariesIllService.HOS_ILL_DETAILS_All:
                //病历获取 详情全部
                illResultReq = (IllResultReq) JsonUtile.json2Obj(json,
                        IllResultReq.class);
                if (StringUtile.isEmpty(illResultReq.illId)) {
                    response = new MsgHint("-1", "病历id不能为空").toJson();
                    break;
                }
                ill = IllsManager1.getInstance().getIlls(getJdbcTemplate(), illResultReq.illId);
                buildRes = IllsManager1.getInstance().getIllBuild(getJdbcTemplate(), illResultReq.illId);
                dataRes = IllsManager1.getInstance().getIllData(getJdbcTemplate(), illResultReq.illId);
                cureRes = IllsManager1.getInstance().getCureEvtity(getJdbcTemplate(), illResultReq.illId);
                resultRes = IllsManager1.getInstance().getIllResult(getJdbcTemplate(), illResultReq.illId);
                historyRes = IllsManager1.getInstance().getIllHistory(getJdbcTemplate(), illResultReq.illId);
                //
                illUpdateReq = new IllUpdateReq();
                illUpdateReq.illId = ill.getIllId();
                illUpdateReq.ill = ill;
                illUpdateReq.illBuild = buildRes;
                illUpdateReq.illData = dataRes;
                illUpdateReq.illCure = cureRes;
                illUpdateReq.illResult = resultRes;
                illUpdateReq.illHistory = historyRes;
                //
                res = new BaseResponse();
                res.obj = illUpdateReq;
                response = res.toJson("0", "");
                break;

        }
        return response;
    }

    //文件上传
    protected String onUploading(String service, String json, UserDocEntity doc, UserPatsEntity pat, UserSysEntity sys) {
        String response = "";
        switch (service) {
            case DictionariesFileService.FILE_UPLOADING_ALI:
                //阿里云上传
                UpLoadingReq upLoadingReq = (UpLoadingReq) JsonUtile.json2Obj(json,
                        UpLoadingReq.class);
                //头像IMAGE_HAED,咨询图片IMAGE_CONSULT,
                //聊天图片IMAGE_CHAT,病情图片ILL_IMAGE,病情视频ILL_VIDEO
                List<String> types = upLoadingReq.types;
                if (types == null || types.size() == 0) {
                    response = new MsgHint("-1", "文件类型不能为空").toJson();
                    break;
                }
                boolean isSuspend = false;
                List<String> keys = new ArrayList();
                for (int i = 0; i < types.size(); i++) {
                    String type = types.get(i);
                    String objKey = getALiObjKey(type);
                    if (StringUtile.isEmpty(objKey)) {
                        response = new MsgHint("-1", "文件类型不能为空").toJson();
                        isSuspend = true;
                        break;
                    }
                    keys.add(objKey);
                }
                if (isSuspend) {
                    break;
                }
                String id = "";
                if (doc != null) {
                    id = doc.getDocId();
                }
                if (pat != null) {
                    id = pat.getPatId();
                }
                if (sys != null) {
                    id = sys.getSysId();
                }
                id += StringUtile.getRandom();
                UploadALiRes res = new UploadALiRes();
                res.bucketName = ConstantData.ALI_YUN_BUCKET_NAME;
                res.endpoint = ConstantData.ALI_YUN_ENDPOINT;
                res.ak = ConstantData.ALI_AK_ID;
                res.aks = ConstantData.ALI_AKS;
                res.objKeys = keys;
                res.objKeyAppend = Md5Utile.encode(id + String.valueOf(System.currentTimeMillis()));
                res.urlFront = "https://" + res.bucketName + "." + res.endpoint + "/";
                //https://hfhos.oss-cn-beijing.aliyuncs.com/images/head/baa0af868ec1fc0bd04089dcb0356280
                BaseResponse baseRes = new BaseResponse();
                baseRes.obj = res;
                response = baseRes.toJson("0", "");
                break;
        }
        return response;
    }

    //头像IMAGE_HAED,咨询图片IMAGE_CONSULT,
    //聊天图片IMAGE_CHAT,病情图片ILL_IMAGE,病情视频ILL_VIDEO
    private String getALiObjKey(String type) {
        String objKey = "";
        switch (type) {
            case "IMAGE_HAED":
                objKey = "images/head/";
                break;
            case "IMAGE_CHAT":
                objKey = "images/chat/";
                break;
            case "IMAGE_CONSULT":
                objKey = "images/consult/";
                break;
            case "ILL_IMAGE":
                objKey = "ill/image/";
                break;
            case "ILL_VIDEO":
                objKey = "ill/video/";
                break;
        }
        return objKey;
    }
}
